/*********************************************************
*
*  Project:      Utilities
*
*  Class:        FlashCardLessonSet
*
*  File name:    FlashCardLessonSet.java
*
*
*    SVN:
*
*    $Rev$:
*    $Date$:
*    $LastChangedBy$:
*
*    $HeadURL$:
*
**********************************************************/

package com.eastmountain.chinese.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Observable;
import java.util.Observer;
import java.util.TreeMap;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.eastmountain.chinese.db.flashcards.records.ChineseCharacterProperties;
import com.eastmountain.chinese.db.flashcards.records.ChineseElementKey;
import com.eastmountain.chinese.db.flashcards.records.ChineseElementProperties;
import com.eastmountain.chinese.db.flashcards.records.ChineseWordProperties;
import com.eastmountain.chinese.db.flashcards.records.Lesson;
import com.eastmountain.chinese.observable.HZFlashcardsDataModel;
import com.eastmountain.util.logging.LoggerUtils;

public class FlashCardLessonSet extends ArrayList<Vector<ChineseElementProperties>>
                                implements Observer {
	
    private HZFlashcardsDataModel hzFlashcardsDataModel = null;
    
    private Class flashCardType;

    Vector<ChineseElementProperties> shuffledFlashCardLessons;
    
    private Vector<ChineseElementProperties> currentLessonFlashCards;

    private int currentLessonIndex = 0;
    private int numberOfLessons    = 0;

    private int currentFlashCardForLessonIndex  = 0;
    private int numberOfFlashCardsForLesson     = 0;
    private int numberOfFlashCardsInSet         = 0;

    private static String projectName = "hzflashcards";
    private static Logger logger = LoggerUtils.setUpLogger( projectName );

    /*************************************************
     *
     *   FlashCardLessonSet
     *
     ************************************************/

    public FlashCardLessonSet() {

        //Vector<ChineseElementProperties> k
        //     = (Vector<ChineseElementProperties>)new Vector<Object>();
        
    }

    public FlashCardLessonSet( Vector<Vector<ChineseElementProperties>> chineseElementPropertiesForLessonList ) {
       
        super( chineseElementPropertiesForLessonList );
        
        hzFlashcardsDataModel = HZFlashcardsDataModel.getInstance();
        
        ChineseElementProperties firstFlashCard = firstFlashCard();
        
        setFlashCardType( firstFlashCard );

    }

    /***********************************************************************************************
     *
     *   add
     *
     **********************************************************************************************/

    @Override
    public boolean add( Vector<ChineseElementProperties> lessonFlashCards ) {

        boolean addResult = super.add( lessonFlashCards );

        numberOfLessons = this.size();

        lessonFlashCards = get( 0 );
        currentLessonFlashCards = lessonFlashCards;

        currentFlashCardForLessonIndex  = 0;
        numberOfFlashCardsForLesson = currentLessonFlashCards.size();
        numberOfFlashCardsInSet     = currentLessonFlashCards.size();

        return( addResult );

    }
    
    public boolean add( TreeMap<String, ChineseElementProperties> chineseElementReviewList ) {

        boolean result;
        
        result = add( new Vector( chineseElementReviewList.values() ) );
        
        return( result )
;    
    }

    /***********************************************************************************************
     *
     *   firstLesson
     *
     **********************************************************************************************/

    public ChineseElementProperties firstFlashCard() {

        System.out.println( "\n------ Do firstFlashCard()" );

        Vector<ChineseElementProperties> lessonFlashCards = null;
        //ChineseElementProperties chineseFlashCard = null;

        currentLessonIndex = 0;

        lessonFlashCards = get( currentLessonIndex );
           
        if ( lessonFlashCards != null ) {

            currentLessonFlashCards = lessonFlashCards;

            numberOfLessons = size();

            currentFlashCardForLessonIndex  = 0;
            numberOfFlashCardsForLesson = currentLessonFlashCards.size();
            numberOfFlashCardsInSet     = currentLessonFlashCards.size();
        
        } else {
        	
        	currentLessonFlashCards = new Vector<ChineseElementProperties>();
        	
        	logger.log( Level.SEVERE, "Unable to retrieve first flashcard" );
        	
        }

        return( currentLessonFlashCards.firstElement() );

    }

    /***********************************************************************************************
     *
     *   previousLesson
     *
     **********************************************************************************************/

    public ChineseElementProperties previousLesson() {
        
        ChineseElementProperties c = new ChineseWordProperties();
        ChineseElementKey k = c;
        
        System.out.println( "\n------ Do previousLesson()" );

        Vector<ChineseElementProperties> lessonFlashCards = null;
        ChineseElementProperties flashCard = null;

        currentLessonIndex--;
        
        if ( numberOfLessons > 1 ) {

        	if ( currentLessonIndex < 0 ) {

        		currentLessonIndex = numberOfLessons - 1;

        	}

        	lessonFlashCards = get( currentLessonIndex );

        	currentLessonFlashCards = lessonFlashCards;
        	
        	numberOfLessons = size();

        	//  reset position in flashcards for this lesson

        	currentFlashCardForLessonIndex  = 0;
        	numberOfFlashCardsForLesson = currentLessonFlashCards.size();

        	flashCard = currentLessonFlashCards.get( currentFlashCardForLessonIndex );
        
        } else {
        	
        	flashCard = previousFlashCard();
        	
        }

        return( flashCard );

    }

    /***********************************************************************************************
     *
     *   nextLesson
     *
     **********************************************************************************************/

    public ChineseElementProperties nextLesson() {

        System.out.println( "\n------ Do nextLesson()" );

        Vector<ChineseElementProperties> lessonFlashCards = null;
        ChineseElementProperties flashCard = null;

        currentLessonIndex++;
        
        if ( numberOfLessons > 1 ) {

        	currentLessonIndex = currentLessonIndex % numberOfLessons;

        	lessonFlashCards = get( currentLessonIndex );

        	currentLessonFlashCards = lessonFlashCards;
        	numberOfLessons = size();

        	//  reset position in flashcards for this lesson

        	currentFlashCardForLessonIndex  = 0;
        	numberOfFlashCardsForLesson = currentLessonFlashCards.size();

        	flashCard = currentLessonFlashCards.get( currentFlashCardForLessonIndex );

        } else {

        	flashCard = nextFlashCard();

        }

        return( flashCard );

    }

    /***********************************************************************************************
     *
     *   previousFlashCard
     *
     **********************************************************************************************/

    public ChineseElementProperties previousFlashCard() {

        System.out.println( "\n------ Do previousFlashCard()" );

        ChineseElementProperties flashCard = null;

        currentFlashCardForLessonIndex--;

        if ( currentFlashCardForLessonIndex < 0 ) {

            if ( numberOfLessons > 1 ) {

                previousLesson();
                
            }  else {

                currentFlashCardForLessonIndex = numberOfFlashCardsForLesson - 1;

            }
            
        }/* else {

            currentFlashCardForLessonIndex = numberOfFlashCardsForLesson - 1;

        }
*/
        flashCard = currentLessonFlashCards.get( currentFlashCardForLessonIndex );

        return( flashCard );

    }

    /***********************************************************************************************
     *
     *   nextFlashCard
     *
     **********************************************************************************************/

    public ChineseElementProperties nextFlashCard() {

        System.out.println( "\n------ Do nextFlashCard()" );

        ChineseElementProperties flashCard = null;

        currentFlashCardForLessonIndex++;

        if ( currentFlashCardForLessonIndex > ( numberOfFlashCardsForLesson - 1 ) ) {

            if ( numberOfLessons > 1 ) {

                nextLesson();

            } else {

                currentFlashCardForLessonIndex = 0;

            }

        } /*else {

            currentFlashCardForLessonIndex = 0;

        }
*/        
        flashCard = currentLessonFlashCards.get( currentFlashCardForLessonIndex );

        return( flashCard );

    }

    /***********************************************************************************************
     *
     *   setLessonStatus
     *
     **********************************************************************************************/

    public void setLessonStatus( ChineseElementProperties chineseElementProperties ) {

    	int lessonNumber;
    	Lesson lesson;
    	LessonStatus lessonStatus;
        	
	    hzFlashcardsDataModel = HZFlashcardsDataModel.getInstance();		
    	lessonNumber = chineseElementProperties.getLessonNumber();
    	lesson = hzFlashcardsDataModel.getLessonForLessonNumber( lessonNumber );

    	lessonStatus = new LessonStatus();

    	lessonStatus.setTitle( lesson.getTitle() );
    	lessonStatus.setLevel( chineseElementProperties.getLevel() );
    	lessonStatus.setLessonNumber( lesson.getLessonNumber() );
    	lessonStatus.setNumberOfLessons( numberOfLessons );
    	lessonStatus.setCardNumberInLesson( currentFlashCardForLessonIndex + 1 );
    	lessonStatus.setNumberOfCardsInLesson( numberOfFlashCardsForLesson );
    	lessonStatus.setNumberOfCardsInSet( numberOfFlashCardsInSet );

    	hzFlashcardsDataModel.setChanged();
    	hzFlashcardsDataModel.notifyObservers( lessonStatus );
    
    }

    /***********************************************************************************************
     *
     *   shuffle
     *
     **********************************************************************************************/

	public Vector<ChineseElementProperties> shuffle() {
		
		System.out.println( "***  FlashCardLessonSet.shuffle()" );

	    shuffledFlashCardLessons = new Vector<ChineseElementProperties>();
		
		for ( Vector<ChineseElementProperties> lessonFlashCards : this ) {
			
			shuffledFlashCardLessons.addAll( lessonFlashCards );
			
		}
		
		Collections.shuffle( shuffledFlashCardLessons );
		
		return( shuffledFlashCardLessons );
		
	}

    /***********************************************************************************************
     *
     *   determineFlashCardType
     *
     **********************************************************************************************/

    public void determineFlashCardType( ChineseElementProperties chineseElementProperties ) {
        
        flashCardType = chineseElementProperties.getClass();

    }

    /***********************************************************************************************
     *
     *   getFlashCardType
     *
     **********************************************************************************************/

    public Class getFlashCardType() {
        return flashCardType;
    }

    /***********************************************************************************************
     *
     *   setFlashCardType
     *
     **********************************************************************************************/

    private void setFlashCardType( ChineseElementProperties chineseElementProperties ) {
        
        flashCardType = chineseElementProperties.getClass();
        
     }


    /***********************************************************************************************
     *
     *   update
     *
     **********************************************************************************************/

	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		
	}

    /***********************************************************************************************
     *
     *   main
     *
     **********************************************************************************************/

    public static void main( Vector<ChineseElementProperties>[] args ) {

        FlashCardLessonSet flashCardLessonSet = new FlashCardLessonSet();

        Vector<ChineseElementProperties> first  = new Vector<ChineseElementProperties>();
        Vector<ChineseElementProperties> second = new Vector<ChineseElementProperties>();
        Vector<ChineseElementProperties> third  = new Vector<ChineseElementProperties>();

        ChineseElementProperties flashCard  = null;

        ChineseCharacterProperties chineseCharacterProperties;

        System.out.println( "\n***  About to test FlashCardLessonSet with an empty list  ***" );

        try {

            flashCardLessonSet.nextLesson();

        } catch ( Exception exception ) {

            System.out.println( "\nnext failed: " + exception.toString() );

        }

        try {

            flashCardLessonSet.previousLesson();

        } catch ( Exception exception ) {

            System.out.println( "\nprevious failed: " + exception.toString() );

        }

        System.out.println( "\n***  About to test FlashCardLessonSet with a populated list  ***" );

        flashCardLessonSet.add( first );
        chineseCharacterProperties = new ChineseCharacterProperties();
        chineseCharacterProperties.setChineseElement( "One" );
        first.add( chineseCharacterProperties );

        flashCardLessonSet.add( second );
        chineseCharacterProperties = new ChineseCharacterProperties();
        chineseCharacterProperties.setChineseElement( "Two" );
        second.add( chineseCharacterProperties );

        flashCardLessonSet.add( third );
        chineseCharacterProperties = new ChineseCharacterProperties();
        chineseCharacterProperties.setChineseElement( "Three" );
        third.add( chineseCharacterProperties );

        System.out.println( "\nSet size " + flashCardLessonSet.size() );

        try {
            Thread.sleep( 2000 );
        } catch ( InterruptedException exception1 ) {

            // TODO Auto-generated catch block
            exception1.printStackTrace();

        }

        try {

            flashCard = flashCardLessonSet.nextFlashCard();

            System.out.println( "\ngot: " + flashCard );

        } catch ( Exception exception ) {

            System.out.println( "\nnext failed: " + exception.toString() );

        }

        try {

            flashCard = flashCardLessonSet.nextLesson();

            System.out.println( "\ngot: " + flashCard );

        } catch ( Exception exception ) {

            System.out.println( "\nnext failed: " + exception.toString() );

        }

        try {

            flashCard = flashCardLessonSet.nextLesson();

            System.out.println( "\ngot: " + flashCard );

        } catch ( Exception exception ) {

            System.out.println( "\nnext failed: " + exception.toString() );

        }

        try {

            flashCard = flashCardLessonSet.previousLesson();

            System.out.println( "\ngot: " + flashCard );


        } catch ( Exception exception ) {

            System.out.println( "\nprevious failed: " + exception.toString() );

        }

        System.out.println();

    }

}
